package com.admin.education.controllers.business;

import com.admin.education.common.exceptions.BadRequestException;
import com.admin.education.common.jwt.JwtAuthorize;
import com.admin.education.common.response.IResponseMessage;
import com.admin.education.common.response.ResponseMessage;
import com.admin.education.common.response.ResponseMessageWithData;
import com.admin.education.common.response.Result;
import com.admin.education.dataTransferObject.PageResponse;
import com.admin.education.dataTransferObject.ServicesExcuteResult;
import com.admin.education.dataTransferObject.question.QuestionTransfer;
import com.admin.education.dataTransferObject.question.Request.CreateQuestionRequest;
import com.admin.education.dataTransferObject.question.Request.QueryQuestionRequest;
import com.admin.education.dataTransferObject.question.Request.UpdateQuestionRequest;
import com.admin.education.dataTransferObject.templates.Request.CreateTemplateRequest;
import com.admin.education.dataTransferObject.templates.Request.QueryTemplateRequest;
import com.admin.education.dataTransferObject.templates.Request.UpdateTemplateRequest;
import com.admin.education.dataTransferObject.templates.TemplatesTransfer;
import com.admin.education.services.Interfaces.ITemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.concurrent.Callable;

@RestController(value = "businessTemplateController")
@JwtAuthorize
@RequestMapping("templates")
@CrossOrigin(origins = "*", maxAge = 3600)
public class TemplateController {
    @Autowired
    private ITemplateService templateService;
    @GetMapping("")
    public Callable<IResponseMessage> queryTemplates(@Valid QueryTemplateRequest queryTemplateRequest) {
        return new Callable<IResponseMessage>() {
            @Override
            public IResponseMessage call() throws Exception {
                IResponseMessage responseMessage = null;

                ServicesExcuteResult<PageResponse<TemplatesTransfer>> servicesExcuteResult = templateService.queryTemplates(queryTemplateRequest);

                if (servicesExcuteResult.isSuccess()) {
                    responseMessage = new ResponseMessageWithData<PageResponse<TemplatesTransfer>>(Result.Success, servicesExcuteResult.getData());
                } else {
                    throw new BadRequestException(servicesExcuteResult.getErrorMessage());
                }
                return responseMessage;
            }
        };
    }

    @GetMapping("{templateId}")
    public Callable<IResponseMessage> queryTemplatesById(@PathVariable Long templateId) {
        return new Callable<IResponseMessage>() {
            @Override
            public IResponseMessage call() throws Exception {
                IResponseMessage responseMessage = null;

                ServicesExcuteResult<TemplatesTransfer> result = templateService.queryTemplateById(templateId);

                if (result.isSuccess()) {
                    responseMessage = new ResponseMessageWithData<TemplatesTransfer>(Result.Success, result.getData());
                } else {
                    throw new BadRequestException(result.getErrorMessage());
                }
                return responseMessage;
            }
        };
    }

    @PostMapping("")
    public Callable<IResponseMessage> createTemplates(@RequestBody CreateTemplateRequest createTemplateRequest) {
        return new Callable<IResponseMessage>() {
            @Override
            public IResponseMessage call() throws Exception {
                IResponseMessage responseMessage = null;

                ServicesExcuteResult result = templateService.createTemplate(createTemplateRequest);

                if (result.isSuccess()) {
                    responseMessage = new ResponseMessage(Result.Success);
                    ;
                } else {
                    throw new BadRequestException(result.getErrorMessage());
                }
                return responseMessage;
            }
        };
    }

    @DeleteMapping("{templateId}")
    public Callable<IResponseMessage> deleteClassesById(@PathVariable Long templateId) {
        return new Callable<IResponseMessage>() {
            @Override
            public IResponseMessage call() throws Exception {
                IResponseMessage responseMessage = null;

                ServicesExcuteResult result = templateService.deleteTemplate(templateId);

                if (result.isSuccess()) {
                    responseMessage = new ResponseMessage(Result.Success);
                    ;
                } else {
                    throw new BadRequestException(result.getErrorMessage());
                }
                return responseMessage;
            }
        };
    }

    @PutMapping("{templateId}")
    public Callable<IResponseMessage> updateTemplates(@PathVariable Long templateId, @RequestBody @Valid UpdateTemplateRequest updateTemplateRequest) {
        return new Callable<IResponseMessage>() {
            @Override
            public IResponseMessage call() throws Exception {
                IResponseMessage responseMessage = null;

                ServicesExcuteResult result = templateService.updateTemplate(templateId, updateTemplateRequest);

                if (result.isSuccess()) {
                    responseMessage = new ResponseMessage(Result.Success);
                    ;
                } else {
                    throw new BadRequestException(result.getErrorMessage());
                }
                return responseMessage;
            }
        };
    }
    @PutMapping("{templateId}/Valid")
    public Callable<IResponseMessage> updateTemplatesValid(@PathVariable Long templateId, @RequestBody @Valid UpdateTemplateRequest updateTemplateRequest) {
        return new Callable<IResponseMessage>() {
            @Override
            public IResponseMessage call() throws Exception {
                IResponseMessage responseMessage = null;

                ServicesExcuteResult result = templateService.updateTemplateValid(templateId);

                if (result.isSuccess()) {
                    responseMessage = new ResponseMessage(Result.Success);
                    ;
                } else {
                    throw new BadRequestException(result.getErrorMessage());
                }
                return responseMessage;
            }
        };
    }
}
